package com.wgo.precise.client.ui.view.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

import remato.client.controller.ModelStatus;
import remato.client.controller.RequirementPlugin;
import remato.client.model.ClientModelItem;
import remato.client.view.util.ReflectionHelper;
import remato.client.view.util.ReflectionRunnableWithProgress;
import remato.client.view.util.ViewerAction;

import com.wgo.bpot.common.removeme.Persistent;
import com.wgo.precise.client.common.session.PreciseSession;
import com.wgo.precise.client.common.session.PreciseSession.ObjectHolder;

public class GetPdfAction extends ViewerAction {

	private FileDialog selectFolderDialog;
	private ClientModelItem item;
	private Shell shell;
	
	public GetPdfAction(Shell shell, ClientModelItem item)
	{
		super("Create " + item.getDomainObjectType().getSimpleName() + " PDF");
		this.item = item;
		this.shell = shell;
		setToolTipText("Create PDF");
		setEnabled(!RequirementPlugin.getInstance().getSession().isDirty());
		setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_DEF_VIEW));
	}

	@Override
	public void run() {
		selectFolderDialog = new FileDialog(shell, SWT.SAVE);
		selectFolderDialog.setFileName("precise_"+item.getSimpleName().trim() + "-" + item.getDomainObjectType().getSimpleName().toLowerCase() +".pdf");
		selectFolderDialog.setFilterExtensions(new String[] { "*.pdf" });
		String fileName = selectFolderDialog.open();
		if (null != fileName) {
			File file = new File(fileName);
			PreciseSession preciseSession = RequirementPlugin.getInstance().getSession().getDomainSession();
			PreciseSession.ObjectHolder<byte[]> objectHolder = new PreciseSession.ObjectHolder<byte[]>();
			IRunnableWithProgress op = new ReflectionRunnableWithProgress("Creating PDF at server and retrieving it.", ReflectionHelper.retrieveMethod(getClass().getSimpleName(), preciseSession.getClass(), "getPdf", Persistent.class, ObjectHolder.class), preciseSession, item.getDomainModelObject(), objectHolder);
			IProgressService progressService = PlatformUI.getWorkbench().getProgressService();
			Throwable throwable = null;
			try {
				progressService.busyCursorWhile(op);
			} catch (InvocationTargetException e) {
				throwable = e.getTargetException();
			} catch (InterruptedException e) {
				throwable = e;
			}
			if (null != throwable) {
				RequirementPlugin.logAndDisplay(new ModelStatus(IStatus.ERROR, "Error creating/retrieveing PDF at/from server.", throwable));
				super.run();
				return;
			}
			FileOutputStream fileOutputStream = null;
			try {
				fileOutputStream = new FileOutputStream(file);
			} catch (FileNotFoundException e) {
				RequirementPlugin.logAndDisplay(new ModelStatus(IStatus.ERROR, e.getMessage(),e));
				super.run();
				return;
			}
			try {
				fileOutputStream.write(objectHolder.getObject());
				fileOutputStream.close();
			} catch (IOException e) {
				RequirementPlugin.logAndDisplay(new ModelStatus(IStatus.ERROR, "Could not write PDF to file",e));
			}
		}		
		super.run();
	}

}
